//package com.demo.api.aop;
//
//import com.demo.api.filter.HeaderMapRequestWrapper;
//import com.demo.api.service.user.IUserService;
//import com.demo.api.vo.user.MenuVo;
//import com.demo.core.annotation.AuthToken;
//import com.demo.core.constant.CommonKey;
//import com.demo.core.enums.ErrorEnum;
//import com.demo.core.exception.ServerException;
//import com.demo.core.utils.StringUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.http.HttpRequest;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.*;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.List;
//import java.util.Objects;
//
///**
// * @author Tan-Bowen
// * @version 1.0
// * @description TODO
// * @date 2021/8/4 16:01
// */
//@Slf4j
//@Aspect
//@Component
//public class AuthTokenAspect {
//
//    @Resource
//    private IUserService userService;
//    /*
//     * @author Tan-Bowen
//     * @description TODO  Spring中使用@Pointcut注解来定义方法切入点
//     * @Pointcut 用来定义切点，针对方法  @Aspect 用来定义切面，针对类
//     * 后面的增强均是围绕此切入点来完成的
//     * 此处仅配置被我们刚才定义的注解：AuthToken修饰的方法即可
//     */
//    @Pointcut("@annotation(authToken)")
//    public void doAuthToken(AuthToken authToken) {
//    }
//
//    /**
//     * 此处我使用环绕增强，在方法执行之前或者执行之后均会执行。
//     */
//    @Around(value = "doAuthToken(authToken)", argNames = "point,authToken")
//    public Object deBefore(ProceedingJoinPoint point, AuthToken authToken) throws Throwable {
//        if(!authToken.checkPower()){
//            return point.proceed();
//        }
//        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
//        if(attributes == null){
//            throw new ServerException(ErrorEnum.NO_AUTHORIZATION);
//        }
//        HttpServletRequest request = attributes.getRequest();
//        String userId = request.getHeader(CommonKey.USER_ID);
//        String uri = request.getRequestURI();
//        if (StringUtils.isBlank(userId)||StringUtils.isBlank(uri)){
//            throw new ServerException(ErrorEnum.ACCESS_DENIED);
//        }
//        List<String> auth = userService.getMenuAndAuth(Long.valueOf(userId)).getAuth();
//        if (auth==null||auth.isEmpty()||!auth.contains(uri)){
//            throw new ServerException(ErrorEnum.ACCESS_DENIED);
//        }
//        return point.proceed();
//    }
//
//
//}
